Public NotInheritable Class frmMain

    '0.9.5
    Private Const CASE_EXT As String = "*.dat"

    Private WithEvents RegionDropDownButton As New ToolStripDropDownButton
    Private WithEvents SectionDropDownButton As New ToolStripDropDownButton
    Private m_intCaseIndex As Integer = 1
    Private m_intPlotIndex As Integer = 1

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        With My.Computer.Screen.WorkingArea
            Me.Size = New Size(.Width, .Height)
        End With
        Me.WindowState = FormWindowState.Maximized
        Me.InitPropertyGrid()
        Me.Translate()
    End Sub

    Public Function BuildedCases() As CaseItem()
        Dim objList As New List(Of CaseItem)
        Dim objCaseItem As CaseItem

        For Each objFATabStripItem As FarsiLibrary.Win.FATabStripItem In Me.TabStrip1.Items
            If objFATabStripItem.GetType Is GetType(CaseItem) Then
                objCaseItem = CType(objFATabStripItem, CaseItem)
                If objCaseItem.Data.Outputs.Updated Then objList.Add(objCaseItem)
            End If
        Next

        Return objList.ToArray
    End Function

#Region "Events"
    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason <> CloseReason.None AndAlso e.CloseReason <> CloseReason.TaskManagerClosing Then
            For intIndex As Integer = Me.TabStrip1.Items.Count - 1 To 0 Step -1
                Dim objFATabStripItem As FarsiLibrary.Win.FATabStripItem = Me.TabStrip1.Items(intIndex)

                If objFATabStripItem.GetType Is GetType(CaseItem) Then
                    Me.TabStrip1.SelectedItem = objFATabStripItem
                    If Me.CanCloseCase Then
                        With Me.CurrentCase.Data.Outputs
                            If .Building Then .Cancel()
                        End With
                        Me.TabStrip1.RemoveTab(objFATabStripItem)
                    Else
                        e.Cancel = True
                        Exit For
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
        With New CustomProfessionalColorTable
            Me.SplitContainer1.BackColor = .MenuStripGradientBegin
            Me.SplitContainer1.Panel1.BackColor = .MenuStripGradientBegin
            Me.SplitContainer1.Panel2.BackColor = .MenuStripGradientBegin
            Me.SplitContainer2.BackColor = .MenuStripGradientBegin
            Me.SplitContainer2.Panel1.BackColor = .MenuStripGradientBegin
            Me.SplitContainer2.Panel2.BackColor = .MenuStripGradientBegin
            Me.lblConsole.BackColor = .ToolStripGradientEnd
            Me.lblProperties.BackColor = .ToolStripGradientEnd
        End With
        Me.TabStrip1.BackColor = Me.BackColor
        Me.UpdateStatus(True)
    End Sub

    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        Me.CurrentTabItem.OnBeginPrint(e)
    End Sub

    Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        Me.CurrentTabItem.OnEndPrint(e)
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Me.CurrentTabItem.OnPrintPage(e)
    End Sub

    Private Sub PropertyGridEx1_PropertyValueChanged(ByVal s As Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles PropertyGridEx1.PropertyValueChanged
        If Me.CurrentCase IsNot Nothing Then Me.CurrentCase.Data.Refresh()
    End Sub

    Private Sub RegionDropDownButton_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles RegionDropDownButton.DropDownItemClicked
        With Me.CurrentCase
            .CurrentRegion = CInt(e.ClickedItem.Tag)
            Me.RegionDropDownButton.Text = Language.ReadMessages("msgREGION") + ": " + .CurrentRegion.ToString
            Me.PropertyGridEx1.SelectedObject = .Data.Sections.Item(.CurrentSection, .CurrentRegion)
        End With
        Me.PropertyGridEx1.ExpandAllGridItems()
    End Sub

    Private Sub SectionDropDownButton_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles SectionDropDownButton.DropDownItemClicked
        With Me.RegionDropDownButton
            .DropDownItems.Clear()
            .Enabled = False
            .Text = Nothing
        End With
        With Me.CurrentCase
            .CurrentSection = CInt(e.ClickedItem.Tag)
            If .CurrentSection = 0 Then
                Me.SectionDropDownButton.Text = Language.ReadMessages("msgSECTION") + ": " + Language.ReadMessages("msgNONE")
                Me.PropertyGridEx1.SelectedObject = .Data
            Else
                Me.SectionDropDownButton.Text = Language.ReadMessages("msgSECTION") + ": " + .CurrentSection.ToString
                If .Data.Sections.HasRegions(.CurrentSection) Then
                    For intIndex As Integer = 1 To .Data.Regions.Count
                        Me.RegionDropDownButton.DropDownItems.Add(intIndex.ToString).Tag = intIndex
                    Next
                    Me.RegionDropDownButton.Enabled = True
                    Me.RegionDropDownButton.DropDownItems.Item(.CurrentRegion - 1).PerformClick()
                Else
                    Me.PropertyGridEx1.SelectedObject = .Data.Sections.Item(.CurrentSection)
                    Me.PropertyGridEx1.ExpandAllGridItems()
                End If
            End If
        End With
    End Sub

    Private Sub TabStrip1_Notification() Handles TabStrip1.Notification
        With Me.txtConsole
            .Text = Me.CurrentTabItem.ConsoleInfo
            .SelectionStart = .Text.Length
            .ScrollToCaret()
        End With
        Me.UpdateStatus(Me.IsReady)
    End Sub

    Private Sub TabStrip1_StatusChanged(ByVal ln As Integer, ByVal col As Integer, ByVal ins As Boolean) Handles TabStrip1.StatusChanged
        Me.UpdateStatus(ln, col, ins)
    End Sub

    Private Sub TabStrip1_TabSelection() Handles TabStrip1.TabSelection
        Me.UpdateControls()
        Me.TabStrip1_Notification()
    End Sub

    Private Sub TabStrip1_TabStripItemClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabStrip1.TabStripItemClosed
        If Me.TabStrip1.Items.Count = 0 Then Me.UpdateControls()
    End Sub

    Private Sub TabStrip1_TabStripItemClosing(ByVal e As FarsiLibrary.Win.TabStripItemClosingEventArgs) Handles TabStrip1.TabStripItemClosing
        If Me.CurrentCase IsNot Nothing Then e.Cancel = Not Me.CanCloseCase
    End Sub

    Private Sub TabStrip1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabStrip1.Validated
        If Me.CurrentCase IsNot Nothing Then Me.ParseText()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        With Me.ToolStripProgressBar1
            If .Value = .Maximum Then
                .Value = 0
            Else
                .PerformStep()
            End If
        End With
        My.Application.DoEvents()
    End Sub
#End Region

#Region "Menu"
    'File

    Private Sub FileNewCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNewCase.Click, btnNew.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.NewCase()
    End Sub

    Private Sub FileOpenCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpenCase.Click, btnOpen.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.OpenCase()
    End Sub

    Private Sub FileSaveCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSaveCase.Click, btnSave.Click
        My.Application.DoEvents()
        If Me.IsReady Then
            Me.ParseText()
            Me.SaveCase()
        End If
    End Sub

    Private Sub FileSaveCaseAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSaveCaseAs.Click
        My.Application.DoEvents()
        If Me.IsReady Then
            Me.ParseText()
            Me.SaveCaseAs()
        End If
    End Sub

    Private Sub FilePageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePageSetup.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.PageSetupDialog1.ShowDialog()
    End Sub

    Private Sub FilePrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrintPreview.Click
        If Me.IsReady Then
            With Me.PrintPreviewDialog1
                .PrintPreviewControl.StartPage = 0
                .WindowState = FormWindowState.Maximized
                If .ShowDialog = DialogResult.OK Then Me.PrintDocument1.Print()
            End With
        End If
    End Sub

    Private Sub FilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
        If Me.IsReady Then Me.PrintDocument1.Print()
    End Sub

    Private Sub FileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        If Me.IsReady Then Me.Close()
    End Sub

    'Edit

    Private Sub EditCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCopy.Click, btnCopy.Click
        If Me.IsReady Then Me.CurrentCase.OnCopy()
    End Sub

    Private Sub EditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click, btnCut.Click
        If Me.IsReady Then Me.CurrentCase.OnCut()
    End Sub

    Private Sub EditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditPaste.Click, btnPaste.Click
        If Me.IsReady Then Me.CurrentCase.OnPaste()
    End Sub

    Private Sub EditSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditSelectAll.Click
        If Me.IsReady Then Me.CurrentCase.OnSelectAll()
    End Sub

    'Case

    Private Sub CaseRegions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCaseRegions.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.ParseText()
        If Me.PropertyGridEx1.Enabled Then
            '0.9.4
            'Me.SectionDropDownButton.DropDownItems.Item(0).PerformClick()
            'If Me.RegionDropDownButton.Enabled Then Me.RegionDropDownButton.DropDownItems.Item(0).PerformClick()
            'With New frmRegions(Me.CurrentCase.Data.Regions)
            '    .ShowDialog()
            'End With
            Dim intRegionCount As Integer = Me.CurrentCase.Data.Regions.Count()

            With New frmRegions(Me.CurrentCase.Data.Regions)
                .ShowDialog()
            End With
            If Me.CurrentCase.Data.Regions.Count <> intRegionCount Then
                Me.CurrentCase.CurrentRegion = 1
                Me.SectionDropDownButton.DropDownItems.Item(0).PerformClick()
            End If
        End If
    End Sub

    Private Sub CaseWavelengths_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCaseWavelengths.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.ParseText()
        If Me.PropertyGridEx1.Enabled Then
            '0.9.4
            'Me.SectionDropDownButton.DropDownItems.Item(0).PerformClick()

            With New frmWavelengths(Me.CurrentCase.Data.Wavelengths)
                .ShowDialog()
            End With
        End If
    End Sub

    Private Sub CaseOutputs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCaseOutputs.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.ParseText()
        If Me.PropertyGridEx1.Enabled Then
            If Me.CurrentCase.Data.Outputs.Updated Then
                '0.9.4
                'Me.SectionDropDownButton.DropDownItems.Item(0).PerformClick()

                With New frmOutputs(Me.CurrentCase.Data.Outputs)
                    .ShowDialog()
                End With
            Else
                MessageBox.Show(Language.ReadOutputs("msgBUILD"), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If
    End Sub

    Private Sub mnuCaseRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuCaseRefresh.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.ParseText()
    End Sub

    'Build

    Private Sub mnuBuildCase_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuBuildCase.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.ParseText()
        If Me.PropertyGridEx1.Enabled Then
            Me.TabStrip1.Enabled = False
            Me.UpdateProperties(True)
            Me.StartProgressBar()
            Me.CurrentCase.ConsoleInfo = ""
            Me.CurrentCase.Data.Outputs.Build()
            If Me.CurrentCase.Data.Outputs.Updated Then
                Me.StopProgressBar(Language.ReadMain("msgBUILDED"))
                Media.SystemSounds.Beep.Play()
            Else
                Me.StopProgressBar()
            End If
            Me.UpdateProperties(False)
            Me.TabStrip1.Enabled = True
        End If
    End Sub

    Private Sub mnuBuildPublish_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuBuildPublish.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.ParseText()
        If Me.PropertyGridEx1.Enabled Then
            With Me.FolderBrowserDialog1
                .Description = Language.ReadMain("msgFOLDER")
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    If My.Computer.FileSystem.GetDirectories(.SelectedPath).Count > 0 OrElse My.Computer.FileSystem.GetFiles(.SelectedPath).Count > 0 Then
                        MessageBox.Show(Language.ReadMain("msgEMPTY"), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Else
                        Me.TabStrip1.Enabled = False
                        Me.UpdateProperties(True)
                        Me.StartProgressBar()
                        Me.CurrentCase.Data.Outputs.Publish(.SelectedPath)
                        If Me.CurrentCase.Data.Outputs.Updated Then
                            Me.StopProgressBar(Language.ReadMain("msgPUBLISHED"))
                            Media.SystemSounds.Beep.Play()
                        Else
                            Me.StopProgressBar()
                        End If
                        Me.UpdateProperties(False)
                        Me.TabStrip1.Enabled = True
                    End If
                End If
            End With
        End If
    End Sub

    Private Sub mnuBuildCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuBuildCancel.Click
        My.Application.DoEvents()
        Me.CurrentCase.Data.Outputs.Cancel()
    End Sub

    'Help

    Private Sub HelpContents_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpContents.Click, btnHelp.Click
        My.Application.DoEvents()
        If Me.IsReady Then Me.OpenHelp()
    End Sub

    Private Sub HelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click
        My.Application.DoEvents()
        frmAbout.ShowDialog()
    End Sub
#End Region

    Private Function CanCloseCase() As Boolean
        If Me.CurrentCase.Data.Outputs.Building Then
            Return False
        ElseIf Not Me.CurrentCase.Data.Saved Then
            Dim result As DialogResult = MessageBox.Show(Language.ReadMain("msgSAVE"), My.Application.Info.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

            My.Application.DoEvents()
            Select Case result
                Case Windows.Forms.DialogResult.Yes
                    Me.SaveCase()
                    Return True

                Case Windows.Forms.DialogResult.No
                    Return True

                Case Windows.Forms.DialogResult.Cancel
                    Return False
            End Select
        Else
            Return True
        End If
    End Function

    Private ReadOnly Property CurrentCase() As CaseItem
        Get
            If Me.TabStrip1.Items.Count = 0 OrElse Me.TabStrip1.SelectedItem.GetType IsNot GetType(CaseItem) Then
                Return Nothing
            Else
                Return CType(Me.TabStrip1.SelectedItem, CaseItem)
            End If
        End Get
    End Property

    Private ReadOnly Property CurrentTabItem() As TabStripItem
        Get
            If Me.TabStrip1.Items.Count = 0 Then
                Return Nothing
            Else
                Return CType(Me.TabStrip1.SelectedItem, TabStripItem)
            End If
        End Get
    End Property

    Private Sub InitPropertyGrid()
        Me.SectionDropDownButton.DisplayStyle = ToolStripItemDisplayStyle.Text
        Me.RegionDropDownButton.DisplayStyle = ToolStripItemDisplayStyle.Text
        With Me.PropertyGridEx1.ToolStrip.Items
            .RemoveAt(4)
            .Add(Me.SectionDropDownButton)
            .Add(New ToolStripSeparator)
            .Add(Me.RegionDropDownButton)
            .Item(3).Visible = False
            .Item(4).Visible = False
            .Item(5).Visible = False
            .Item(6).Visible = False
        End With
        With Me.SectionDropDownButton.DropDownItems
            .Add(Language.ReadMain("msgCASE")).Tag = 0
            For intIndex As Integer = 1 To 10
                .Add(Language.ReadMessages("msgSECTION" + intIndex.ToString)).Tag = intIndex
            Next
        End With
    End Sub

    Private Function IsReady() As Boolean
        Return Not Me.Timer1.Enabled
    End Function

    Private Sub NewCase()
        Dim strDefaultCase As String
        Dim objCaseItem As CaseItem

        With My.Computer.FileSystem
            strDefaultCase = .CombinePath(My.Application.Info.DirectoryPath, "Default.dat")
            If .FileExists(strDefaultCase) Then
                Me.OpenCase(strDefaultCase)
                Me.CurrentCase.Title = Language.ReadMain("msgCASE") + m_intCaseIndex.ToString
                m_intCaseIndex += 1
                Exit Sub
            End If
        End With

        Me.StartProgressBar()

        objCaseItem = New CaseItem(Language.ReadMain("msgCASE") + m_intCaseIndex.ToString)
        Me.TabStrip1.AddTab(objCaseItem)
        m_intCaseIndex += 1

        Me.StopProgressBar()

        Me.UpdateControls()

        objCaseItem.ReadOnly = False
    End Sub

    Private Overloads Sub OpenCase()
        With Me.OpenFileDialog1
            '0.9.6
            .CheckFileExists = True

            .FileName = ""
            .Filter = Language.ReadMain("msgFILES") + "|" + CASE_EXT

            '0.9.5
            'If .ShowDialog(Me) = DialogResult.OK Then Me.OpenCase(.FileName)
            If .ShowDialog(Me) = DialogResult.OK Then
                For Each item As CaseItem In Me.TabStrip1.Items
                    If item.Data.File = .FileName Then
                        Me.TabStrip1.SelectedItem = item
                        Exit Sub
                    End If
                Next
                Me.OpenCase(.FileName)
            End If
        End With
    End Sub

    Private Overloads Sub OpenCase(ByVal file As String)
        Dim strErrorMsg As String = Nothing
        Dim objCaseItem As CaseItem

        Me.StartProgressBar()

        objCaseItem = New CaseItem(Language.ReadMain("msgLOADING"))
        Me.TabStrip1.AddTab(objCaseItem)
        Try
            objCaseItem.Data.Open(file)
        Catch ex As Exception
            strErrorMsg = ex.Message
        End Try

        Me.StopProgressBar()

        Me.UpdateControls()

        If strErrorMsg IsNot Nothing Then Me.ShowError(strErrorMsg, objCaseItem.Data.Sections.ErrorLineNumber)

        objCaseItem.ReadOnly = False
    End Sub

    Private Sub OpenHelp()
        Process.Start("http://damps.codeplex.com/documentation")
    End Sub

    Private Sub ParseText()
        Dim strErrorMsg As String = Nothing

        With Me.CurrentCase
            If .Data IsNot Nothing Then
                Me.TabStrip1.UseWaitCursor = True

                Try
                    .Data.Sections.Write(.Text)
                Catch ex As MySections.SectionException
                    strErrorMsg = ex.Message
                End Try

                Me.TabStrip1.UseWaitCursor = False

                Me.UpdateProperties()

                If Not (strErrorMsg Is Nothing) Then Me.ShowError(strErrorMsg, .Data.Sections.ErrorLineNumber)
            End If
        End With
    End Sub

    Private Sub SaveCase()
        If Me.CurrentCase.Data.File Is Nothing Then
            Me.SaveCaseAs()
        Else
            Dim strErrorMsg As String = Nothing

            Me.StartProgressBar()

            Try
                Me.CurrentCase.Data.Save()
            Catch ex As MyCase.CaseException
                strErrorMsg = ex.Message
            End Try

            Me.StopProgressBar()

            If Not (strErrorMsg Is Nothing) Then Me.ShowError(strErrorMsg)
        End If
    End Sub

    Private Sub SaveCaseAs()
        Dim strErrorMsg As String = Nothing
        Dim strFileName As String

        With Me.SaveFileDialog1
            '0.9.6
            '.CheckFileExists = True
            .CheckPathExists = True

            .FileName = ""
            .Filter = Language.ReadMain("msgFILES") + "|" + CASE_EXT
            If .ShowDialog(Me) = DialogResult.OK Then
                strFileName = .FileName
            Else
                Exit Sub
            End If
        End With

        Me.StartProgressBar()

        Try
            Me.CurrentCase.Data.Save(strFileName)
        Catch ex As MyCase.CaseException
            strErrorMsg = ex.Message
        End Try

        Me.StopProgressBar()

        Me.UpdateTitle()

        If Not (strErrorMsg Is Nothing) Then Me.ShowError(strErrorMsg)
    End Sub

    Private Overloads Sub ShowError(ByVal msg As String)
        MessageBox.Show(msg, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub

    Private Overloads Sub ShowError(ByVal msg As String, ByVal line As Long)
        MessageBox.Show(String.Format(Language.ReadMain("msgERROR"), line) + ": " + msg, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub

    Private Sub StartProgressBar()
        'Me.UseWaitCursor = True
        With Me.ToolStripProgressBar1
            .Value = 0
            .Visible = True
        End With
        Me.UpdateStatus(False)
        Me.Timer1.Enabled = True
    End Sub

    Private Sub StopProgressBar(Optional ByVal status As String = Nothing)
        Me.Timer1.Enabled = False
        Me.ToolStripProgressBar1.Visible = False
        Me.UpdateStatus(True, status)
        'Me.UseWaitCursor = False
    End Sub

    Private Sub Translate()
        'Menu bar
        Me.mnuFile.Text = Language.ReadMain("mnuFile")
        Me.mnuFileNewCase.Text = Language.ReadMain("mnuFileNewCase")
        Me.mnuFileOpenCase.Text = Language.ReadMain("mnuFileOpenCase")
        Me.mnuFileSaveCase.Text = Language.ReadMain("mnuFileSaveCase")
        Me.mnuFileSaveCaseAs.Text = Language.ReadMain("mnuFileSaveCaseAs")
        Me.mnuFilePageSetup.Text = Language.ReadMain("mnuFilePageSetup")
        Me.mnuFilePrintPreview.Text = Language.ReadMain("mnuFilePrintPreview")
        Me.mnuFilePrint.Text = Language.ReadMain("mnuFilePrint")
        Me.mnuFileExit.Text = Language.ReadMain("mnuFileExit")
        Me.mnuEdit.Text = Language.ReadMain("mnuEdit")
        Me.mnuEditUndo.Text = Language.ReadMain("mnuEditUndo")
        Me.mnuEditRedo.Text = Language.ReadMain("mnuEditRedo")
        Me.mnuEditCut.Text = Language.ReadMain("mnuEditCut")
        Me.mnuEditCopy.Text = Language.ReadMain("mnuEditCopy")
        Me.mnuEditPaste.Text = Language.ReadMain("mnuEditPaste")
        Me.mnuEditSelectAll.Text = Language.ReadMain("mnuEditSelectAll")
        Me.mnuCaseRegions.Text = Language.ReadMain("mnuCaseRegions")
        Me.mnuCaseOutputs.Text = Language.ReadMain("mnuCaseOutputs")
        Me.mnuCaseWavelengths.Text = Language.ReadMain("mnuCaseWavelengths")
        Me.mnuCaseRefresh.Text = Language.ReadMain("mnuCaseRefresh")
        Me.mnuBuildCase.Text = Language.ReadMain("mnuBuildCase")
        Me.mnuBuildPublish.Text = Language.ReadMain("mnuBuildPublish")
        Me.mnuBuildCancel.Text = Language.ReadMain("mnuBuildCancel")
        Me.mnuHelp.Text = Language.ReadMain("mnuHelp")
        Me.mnuHelpContents.Text = Language.ReadMain("mnuHelpContents")
        Me.mnuHelpAbout.Text = String.Format(Language.ReadMain("mnuHelpAbout"), My.Application.Info.ProductName)

        'Button bar
        Me.btnNew.Text = Language.ReadMain("btnNew")
        Me.btnOpen.Text = Language.ReadMain("btnOpen")
        Me.btnSave.Text = Language.ReadMain("btnSave")
        Me.btnCut.Text = Language.ReadMain("btnCut")
        Me.btnCopy.Text = Language.ReadMain("btnCopy")
        Me.btnPaste.Text = Language.ReadMain("btnPaste")
        Me.btnHelp.Text = Language.ReadMain("btnHelp")

        'Windows
        Me.lblConsole.Text = Language.ReadMain("lblConsole")
        Me.lblProperties.Text = Language.ReadMain("lblProperties")
    End Sub

    Private Sub UpdateControls()
        Me.UpdateTitle()
        Me.UpdateMenu()
        Me.UpdateProperties()
    End Sub

    Private Sub UpdateMenu()
        Dim blnEnabled As Boolean = Me.CurrentCase IsNot Nothing

        Me.mnuFileSaveCase.Enabled = blnEnabled
        Me.mnuFileSaveCaseAs.Enabled = blnEnabled
        Me.btnSave.Enabled = blnEnabled
        Me.mnuFilePrint.Enabled = Me.CurrentTabItem IsNot Nothing
        Me.mnuFilePrintPreview.Enabled = Me.CurrentTabItem IsNot Nothing
        Me.mnuEditCut.Enabled = blnEnabled
        Me.mnuEditCopy.Enabled = blnEnabled
        Me.mnuEditPaste.Enabled = blnEnabled
        Me.mnuEditSelectAll.Enabled = blnEnabled
        Me.mnuCaseRegions.Enabled = blnEnabled
        Me.mnuCaseOutputs.Enabled = blnEnabled
        Me.mnuCaseWavelengths.Enabled = blnEnabled
        Me.mnuCaseRefresh.Enabled = blnEnabled
        Me.mnuBuildCase.Enabled = blnEnabled
        Me.mnuBuildPublish.Enabled = blnEnabled
        Me.btnCut.Enabled = blnEnabled
        Me.btnCopy.Enabled = blnEnabled
        Me.btnPaste.Enabled = blnEnabled
        My.Application.DoEvents()

        If Not blnEnabled Then Me.UpdateStatus(False)
    End Sub

    Private Overloads Sub UpdateProperties()
        If Me.CurrentCase Is Nothing OrElse Me.CurrentCase.Data Is Nothing Then
            With Me.PropertyGridEx1.ToolStrip
                .Items(3).Visible = False
                .Items(4).Visible = False
                .Items(5).Visible = False
                .Items(6).Visible = False
            End With
            Me.RegionDropDownButton.DropDownItems.Clear()
            Me.PropertyGridEx1.SelectedObject = Nothing
        Else
            With Me.PropertyGridEx1.ToolStrip
                .Items(3).Visible = True
                .Items(4).Visible = True
                .Items(5).Visible = True
                .Items(6).Visible = True
            End With
            Me.UpdateProperties(False)
        End If
    End Sub

    Private Overloads Sub UpdateProperties(ByVal building As Boolean)
        If building Then
            Me.PropertyGridEx1.Enabled = False
            Me.SectionDropDownButton.DropDownItems.Item(0).PerformClick()
            Me.StartProgressBar()
            Me.mnuBuildCase.Enabled = False
            Me.mnuBuildPublish.Enabled = False
            Me.mnuBuildSeparator1.Visible = True
            Me.mnuBuildCancel.Visible = True
        Else
            If Me.CurrentCase.Data.Sections.ErrorLineNumber = 0 Then
                Me.PropertyGridEx1.Enabled = True
                Me.SectionDropDownButton.DropDownItems.Item(Me.CurrentCase.CurrentSection).PerformClick()
            Else
                Me.PropertyGridEx1.Enabled = False
            End If
            Me.mnuBuildCase.Enabled = True
            Me.mnuBuildPublish.Enabled = True
            Me.mnuBuildSeparator1.Visible = False
            Me.mnuBuildCancel.Visible = False
        End If
    End Sub

    Private Overloads Sub UpdateStatus(ByVal ready As Boolean, Optional ByVal status As String = Nothing)
        Me.PropertyGridEx1.Enabled = ready
        If ready Then
            If status Is Nothing Then
                Me.ToolStripStatusLabel1.Text = Language.ReadMain("msgREADY")
            Else
                Me.ToolStripStatusLabel1.Text = Language.ReadMain("msgREADY") + " (" + status + ")"
            End If
        Else
            Me.ToolStripStatusLabel1.Text = status
            Me.ToolStripStatusLabel2.Text = Nothing
            Me.ToolStripStatusLabel3.Text = Nothing
            Me.ToolStripStatusLabel4.Text = Nothing
        End If
        My.Application.DoEvents()
    End Sub

    Private Overloads Sub UpdateStatus(ByVal ln As Integer, ByVal col As Integer, ByVal ins As Boolean)
        Me.ToolStripStatusLabel2.Text = Language.ReadMain("msgLN") + " " + ln.ToString
        Me.ToolStripStatusLabel3.Text = Language.ReadMain("msgCOL") + " " + col.ToString
        If ins Then
            Me.ToolStripStatusLabel4.Text = Language.ReadMain("msgINS")
        Else
            Me.ToolStripStatusLabel4.Text = Language.ReadMain("msgOVR")
        End If
        My.Application.DoEvents()
    End Sub

    Private Sub UpdateTitle()
        Me.Text = My.Application.Info.ProductName
        If Me.CurrentCase IsNot Nothing AndAlso Me.CurrentCase.Data IsNot Nothing AndAlso Me.CurrentCase.Data.File IsNot Nothing Then
            Me.Text += " - " + Me.CurrentCase.Data.File
        End If
        My.Application.DoEvents()
    End Sub

End Class
